package com.fm.structure.tree;

import com.fm.structure.hash.BasicHashSet;

import java.util.Set;

/**
 * @author finalmachine
 */
public class BasicTreeNode<E> {
    private E element;
    private Set<BasicTreeNode<E>> children;

    public BasicTreeNode(E element) {
        this.element = element;
    }

    public E getElement() {
        return element;
    }

    public void setElement(E element) {
        this.element = element;
    }

    public Set<BasicTreeNode<E>> getChildren() {
        return children;
    }

    public void setChildren(Set<BasicTreeNode<E>> children) {
        this.children = children;
    }

    public void addChild(E element) {
        if (children == null) {
            children = new BasicHashSet<>();
        }
        children.add(new BasicTreeNode<>(element));
    }

    @Override
    public int hashCode() {
        return element.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        return o != null
            && o instanceof BasicTreeNode
            && ((BasicTreeNode<?>) o).getElement().equals(element);
    }
}
